﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.CommandLineUtilities
{
    [Serializable]
    public class CommandLineOutput:ResultBase
    {
        private Dictionary<string,string> _CommandLineAppOutputs;
        /// <summary></summary>
        public Dictionary<string,string> CommandLineAppOutputs
        {
            get
            {
                return this._CommandLineAppOutputs;
            }
            set
            {
                this._CommandLineAppOutputs = value;
            }
        }

        public CommandLineOutput():base(Guid.Empty,Guid.Empty)
        {
            this._CommandLineAppOutputs = null;
        }

        public CommandLineOutput(Guid componentID, Guid jobTicketID, Dictionary<string,string> commandLineOutputs) 
            : base(componentID, jobTicketID)
        {
            this._CommandLineAppOutputs = commandLineOutputs;
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            if(this.CommandLineAppOutputs !=null && this.CommandLineAppOutputs.Count>0)
            {
                XmlNode outputRootNode = XmlDataUtil.AddElement(ref xDoc, xNode, "Outputs");
                foreach(string name in this.CommandLineAppOutputs.Keys)
                {
                    XmlNode outputNode = XmlDataUtil.AddElement(ref xDoc, outputRootNode, "Output");
                    XmlDataUtil.UpdateAttribute(ref xDoc,outputNode,"Name",name);
                    XmlDataUtil.UpdateAttribute(ref xDoc, outputNode, "Value", this.CommandLineAppOutputs[name]);
                }
            }

            return xNode;
        }

        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                CommandLineOutput output = new CommandLineOutput();
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                output.CommandLineAppOutputs = null;
                XmlNodeList outputNodes = xNode.SelectNodes("Outputs/Output");
                if (outputNodes != null && outputNodes.Count > 0)
                {
                    output.CommandLineAppOutputs=new Dictionary<string, string>();
                    foreach (XmlNode outputNode in outputNodes)
                    {
                        string key = outputNode.Attributes.GetNamedItem("Name").Value;
                        string value = outputNode.Attributes.GetNamedItem("Value").Value;
                        output.CommandLineAppOutputs.Add(key,value);
                    }
                }

                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }
    }
}
